package bl.document;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import po.DocumentPo;
import po.Po;
import po.SystemLogPo;
import po.库存报损报溢单po;
import vo.DocumentVo;
import vo.Vo;
import vo.StockScrapAndOverflowDocumentVo;
import dbservice.document.DocumentDBService;
import dbservice.history.SystemLogDBService;

public class StockScrapAndOverflowDocumentBL extends DocumentBL {
	
	private static final long serialVersionUID = 8912353639854561207L;


	public StockScrapAndOverflowDocumentBL(DocumentDBService documentDB,SystemLogDBService log) throws RemoteException {
		super(documentDB,log);
		// TODO Auto-generated constructor stub
	}


	public StockScrapAndOverflowDocumentVo _vo(库存报损报溢单po po){
		   StockScrapAndOverflowDocumentVo vo=new StockScrapAndOverflowDocumentVo();
		   vo.set单据编号(po.get单据编号());
		   vo.set商品名称(po.get商品名称());
		   vo.set商品编号(po.get商品编号());
		   vo.set商品型号(po.get商品型号());
		   vo.setNum(po.getNum());
		   vo.set备注(po.get备注());
		   vo.setOperator(po.get操作员());
		   vo.setType(po.isType());
		   return vo;
	   }
	   public 库存报损报溢单po _po(StockScrapAndOverflowDocumentVo vo){
		  库存报损报溢单po po=new 库存报损报溢单po();	  
		  po.set单据编号(vo.get单据编号());
		  po.set商品名称(vo.get商品名称());
		  po.set商品型号(vo.get商品型号());
		  po.set商品编号(vo.get商品编号());
		  po.setNum(vo.getNum());
		  po.set备注(vo.get备注());
		  po.set操作员(vo.getOperator());
		  po.setType(vo.isType());
		  return po;
	   }
	   public DocumentPo _po(DocumentVo vo){
		   DocumentPo po=new DocumentPo();
		   po.set客户(vo.get客户());
		   po.setDate(vo.getDate());
		   po.set业务员(vo.get业务员());
		   po.setType(vo.isType());
		   po.set仓库(vo.get仓库());
		   return po;
	   }
		@Override
		public Vo approve(DocumentVo document) {
			Po po=documentDB.approve(_po((StockScrapAndOverflowDocumentVo)document));
			Vo vo=new Vo();
			if(po.isSuccessed()){
				vo.successed();
				log.add(new SystemLogPo("总经理", "审批",((StockScrapAndOverflowDocumentVo)document).get单据编号() ));
			}else{
				vo.failed(po.getFailMessage());
			}
			return vo;
		}

		@Override
		public Vo add(DocumentVo document) {
			Po po=documentDB.add(_po((StockScrapAndOverflowDocumentVo) document));
			Vo vo=new Vo();
			if(po.isSuccessed()){
				vo.successed();
				log.add(new SystemLogPo("库存管理人员", "添加","库存报损报溢单" ));
			}
			else{
			    vo.failed(po.getFailMessage());
			}
			return vo;
		}

		@Override
		public Vo update(DocumentVo document) {
			Po po=documentDB.update(_po((StockScrapAndOverflowDocumentVo) document));
			Vo vo=new Vo();
			if(po.isSuccessed()){
				vo.successed();
			}
			else{
			    vo.failed(po.getFailMessage());
			}
			return vo;
		}

		@Override
		public List<DocumentVo> get(DocumentVo from, DocumentVo to) {
			List<DocumentPo> po=documentDB.get(_po(from), _po(to));
			List<DocumentVo> vo=new ArrayList<DocumentVo> ();
			for(int i=0;i<po.size();i++){
				vo.add(_vo((库存报损报溢单po) po.get(i)));
			}
			return vo;
		}

		@Override
		public List<DocumentVo> getUnapproval() {
			List<DocumentPo> po=documentDB.getUnapproval();
			List<DocumentVo> vo=new ArrayList<DocumentVo> ();
			for(int i=0;i<po.size();i++){
				vo.add(_vo((库存报损报溢单po) po.get(i)));
			}
			return vo;
		}


		@Override
		public Vo redRush(DocumentVo vo) {
			Vo m=new Vo();
			((StockScrapAndOverflowDocumentVo)vo).setNum(-((StockScrapAndOverflowDocumentVo)vo).getNum());
			Vo r=add(vo);
			if(r.isSuccessed()){
				m.successed();
				log.add(new SystemLogPo(((StockScrapAndOverflowDocumentVo)vo).getOperator(), "红冲",((StockScrapAndOverflowDocumentVo)vo).get单据编号() ));
			}else{
				m.failed(r.getFailMessage());
			}
			
			return m;
			
		}


}
